package leetcode.od;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author pppppp
 * @date 2022/3/30 10:05
 * 给定2D空间中四个点的坐标 p1, p2, p3 和 p4，如果这四个点构成一个正方形，则返回 true 。
 * 点的坐标 pi 表示为 [xi, yi] 。输入 不是 按任何顺序给出的。
 * 一个 有效的正方形 有四条等边和四个等角(90度角)。

 * 示例 1:
 * 输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
 * 输出: True
 *
 * 示例 2:
 * 输入：p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
 * 输出：false
 *
 * 示例 3:
 * 输入：p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1]
 * 输出：true

 * 提示:
 * p1.length == p2.length == p3.length == p4.length == 2
 * -104 <= xi, yi <= 104
 */
public class _593_有效的正方形 {
    @Test
    public void T_0() {
        int[][][] nums = {{{0, 0}, {1,1}, {0, 1},{1,0}},{{1, 0}, {-1,0}, {0, 1},{0,-1}},{{0, 0}, {1,1}, {0, 1},{0,12}}, {{0, 0}, {1, 2}, {3, 1}, {2, -1}}};
        boolean[] ans = {true, true,false,true};
        for (int i = 0; i < nums.length; i++) {
            // System.out.println(validSquare(nums[i][0],nums[i][1],nums[i][2],nums[i][3]) == ans[i]);
            System.out.println(validSquare_1(nums[i][0],nums[i][1],nums[i][2],nums[i][3]) == ans[i]);
        }
    }

        /*优化-- 排序后就能确定四个点的相对位置  判断4条边相等 且 对角线相等*/
        public boolean validSquare_1(int[] p1, int[] p2, int[] p3, int[] p4){
            /*按照x升序 y升序*/
            int[][] p={p1,p2,p3,p4};
            Arrays.sort(p, (o1, o2) -> {
                if(o1[0] == o2[0]){
                    return o1[1] - o2[1];
                }
                return o1[0] - o2[0];
            });
            return distance(p[0],p[1]) > 0 && distance(p[0],p[1]) == distance(p[0],p[2])
                    && distance(p[3],p[1]) == distance(p[3],p[2])
                    && distance(p[0],p[1]) == distance(p[1],p[3])
             && distance(p[0],p[3]) == distance(p[1],p[2]);
        }

        private int distance(int[] p0, int[] p1) {
            return (p0[0] - p1[0]) * (p0[0] - p1[0]) + (p0[1] - p1[1]) * (p0[1] - p1[1]);
        }

    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        int[][] points = new int[4][2];
        points[0] = p1;
        points[1] = p2;
        points[2] = p3;
        points[3] = p4;
        for (int i = 0; i < 4; i++) {
            for (int j = i+1; j < 4; j++) {
                for (int k = j+1; k < 4; k++) {
                    int x1 = (points[i][0] - points[j][0]) * (points[i][0] - points[j][0]);
                    int y1 = (points[i][1] - points[j][1]) * (points[i][1] - points[j][1]);
                    int x2 = (points[k][0] - points[j][0]) * (points[k][0] - points[j][0]);
                    int y2 = (points[k][1] - points[j][1]) * (points[k][1] - points[j][1]);
                    if(x1 + y1 == 0 || x2 + y2 == 0){
                        return false;
                    }
                    if(x1 + y1 != x2 + y2 && x1 + y1 != 2*(x2 + y2) && 2*(x1 + y1) != x2 + y2){
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
